package offer;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class offerII_0046 {
    public List<Integer> rightSideView(TreeNode root) {
        LinkedList<TreeNode> list=new LinkedList<>();
        List<Integer> result=new ArrayList<>();
        if(root==null) return result;
        int current=0;
        int right=root.val;
        int number=1;
        list.add(root);
        while(number>0){
            for(int i=0;i<number;++i){
                if(i==0) {right=list.getFirst().val;result.add(right);}
                TreeNode node=list.removeFirst();
                if(node.right!=null) {
                    list.add(node.right);
                    current++;
                }
                if(node.left!=null){
                    list.add(node.left);
                    current++;
                }
            }
            number=current;
            current=0;
        }
        return result;
    }
}
